// pages/Home/index.js
/**
 * 小程序首页 
 * 
 * create & edit by cyj at 2024-09-10
 * */
import {
    showToast,
    getParams,
    logger
} from '@/utils/util'
import { customNavigate } from '@/utils/route'
import {
    fetchNoticeList,
    fetchDeviceList
} from '@/services/home'
const app = getApp()
Page({
    data: {
        paddingTop: app.globalData.STATUS_BAR_HEIGHT + app.globalData.NAVIGATE_BAR_HEIGHT,
        paddingBottom: app.globalData.IPHONE_BAR_HEIGHT,
        staticPrefix: app.globalData.STATIC_PREFIX,
        menus: [],
        device_list: [],
        health_list: [],
        safety_list: [],
        notice_list: [],
        plan_list: [],
        user_info: {},
        elderly_info: {},
        height: 0
    },
    onLoad(options) {},
    onShow() {
        if (!wx.getStorageSync('elderlyInfo')) {
            showToast('请选择平台用户', () => {
                customNavigate({
                    url: '/User/pages/Elderly/index'
                })
            })
        } else {
            this.setData({
                elderly_info: wx.getStorageSync('elderlyInfo')
            }, () => {
                this.searchDeviceList();
                this.searchNoticeList();
            })
        }
    },
    async searchNoticeList() {
        const params = {
            page_size: 10,
            page_num: 1,
            device_type: 1000,
            warn_type: '全部',
            id: this.data.elderly_info.id,
        }
        const res = await fetchNoticeList(params);
        if (res.return_code == 'SUCCESS') {
            let notices = [];
            if (res.data?.length > 2) {
                notices = res.data.splice(0, 2)
            }
            this.setData({
                notices
            })
        }
    },
    async searchDeviceList() {
        let params = {
                id: this.data.elderly_info.id
            },
            menus = [{
                path: '/Device/pages/List/index',
                src: `${app.globalData.STATIC_PREFIX}/nav/icon_list.png`,
                name: '设备列表'
            }, {
                path: '/Extra/pages/HealthPlan/index',
                src: `${app.globalData.STATIC_PREFIX}/nav/icon_health.png`,
                name: '健康计划'
            }, {
                path: '/Extra/pages/CallCenter/index',
                src: `${app.globalData.STATIC_PREFIX}/nav/icon_call.png`,
                name: '一键呼叫'
            }];
        const res = await fetchDeviceList(params);
        if (res.return_code == 'SUCCESS') {
            this.setData({
                health_list: res.data.health_list,
                safety_list: res.data.home_list,
                device_list: res.data.health_list.concat(res.data.home_list),
            }, () => {
                const haveWatch = res.data.health_list.concat(res.data.home_list).some((item) => item.device_type == '0' && (item.product_type == 'X1' || item.product_type == 'X1_S' || item.product_type == 'X5'));
                this.setData({
                    menus: !haveWatch ? menus.filter((item) => item.name != '健康计划') : menus
                })
                wx.setStorageSync('deviceList', [...res.data.health_list, ...res.data.home_list])
            })
        }
    },
    onNavigate(e) {
        const params = {
            device_imei: e.currentTarget.dataset.item.device_imei
        }
        logger.log(params)
        customNavigate({
            url: '/Device/pages/Info/index',
            params
        })
    },
    onRoute(e) {
        let path = e.currentTarget.dataset.path,
            watch = this.data.device_list.filter((item) => item.device_type == 0),
            routeParams;
        switch (path) {
            case '/Extra/pages/HealthPlan/index':
                routeParams = {
                    url: path,
                    params: {
                        device_imei: watch[0].device_imei,
                        watch_info_id: watch[0].watch_info_id
                    }
                }
                break;
            case '/Extra/pages/CallCenter/index':
                routeParams = {
                    url: path
                }
                break;
            case '/Extra/pages/MessageNotice/index':
                routeParams = {
                    url: path,
                }
            case '/Device/pages/Bind/List/index':
                routeParams = {
                    url: path
                }
                break;
            case '/User/pages/Elderly/index':
                routeParams = {
                    url: path
                }
                break;
            case '/Device/pages/List/index':
                routeParams = {
                    url: path
                }
                break;
            default:
                break;
        }
        customNavigate({
            ...routeParams
        })
    },
    onReady() {},
    onHide() {},
    onUnload() {},
    onPullDownRefresh() {},
    onReachBottom() {},
    onShareAppMessage() {}
})